package com.ncloud.resman.busi;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ncloud.common.exception.UserException;
import com.ncloud.common.utils.DateUtils;
import com.ncloud.common.utils.excel.ExcelUtil;
import com.ncloud.resman.entity.DeviceSub;
import com.ncloud.resman.entity.ExportIpAddress;
import com.ncloud.resman.entity.ImportIpAddress;
import com.ncloud.resman.entity.IpAddress;
import com.ncloud.resman.mapper.DictMapper;
import com.ncloud.resman.mapper.IpAddressMapper;
import com.ncloud.resman.mapper.SequenceMapper;
import com.ncloud.resman.service.NetworkExcelServer;


/**
 * user:sundelong  date:2017年8月30日
 */
@Service
public class NetWorkExcelBusi {
	@Autowired
	private NetworkExcelServer networkExcelServer;
	@Autowired
	private IpAddressMapper ipAddressMapper;
	@Autowired
	private SequenceMapper sequenceMapper;
	@Autowired
	private DictMapper dictMapper;
	
	//导出IP地址列表
	 public void exportIpAdress(JSONObject json,HttpServletResponse response) throws IOException{
	    String netType = json.getString("netType");
		String ipSegment = json.getString("ipSegment");
		String state = json.getString("state");
		String ip = json.getString("ip");
		List<ExportIpAddress> ipAddrsInfo = new ArrayList<ExportIpAddress>();
		ipAddrsInfo=networkExcelServer.getIpAddressList(netType, ipSegment, state, ip);
	    JSONArray listInfo = JSON.parseArray(JSON.toJSONString(ipAddrsInfo));
	 
	    Date date=new Date();
	    SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMssHHddmm");
	    OutputStream os = null;
		String fileName = "IP地址列表_"+sdf.format(date)+".xls";
		response.setContentType("application/vnd.ms-excel");
	    response.setHeader("Content-disposition", "attachment;filename="+new String(fileName.getBytes("gb2312"), "ISO-8859-1"));
	    os = response.getOutputStream();
		Map<String, String> map = new HashMap<String, String>();
	    map.put("total", listInfo.size()+" 条");
	    map.put("date", DateUtils.getDate());
	    ExcelUtil.getInstance().exportObj2ExcelByTemplate(map, "ipAddress-info-template.xls",os, listInfo, ExportIpAddress.class, true);
			   	
	    }
	//导出IP地址列表模板
	public void exportIpAdressTemplate(JSONObject json,HttpServletResponse response) throws IOException{
		List<ImportIpAddress> ipAddrsInfo = new ArrayList<ImportIpAddress>();

		JSONArray listInfo = JSON.parseArray(JSON.toJSONString(ipAddrsInfo));
		 
		OutputStream os = null;
		String fileName = "IP地址列表_"+"导入模板"+".xls";
		response.setContentType("application/vnd.ms-excel");
		response.setHeader("Content-disposition", "attachment;filename="+new String(fileName.getBytes("gb2312"), "ISO-8859-1"));
		os = response.getOutputStream();
		Map<String, String> map = new HashMap<String, String>();
		map.put("total", ipAddrsInfo.size()+" 条");
		map.put("date", DateUtils.getDate());
		ExcelUtil.getInstance().exportObj2ExcelByTemplate(map, "ipAddress-info-template.xls",os, listInfo, ImportIpAddress.class, true);
				   	
	}
	 
	//导入交换机接口EXCEL
	 public JSONObject importIpAddressInfo(InputStream inputStream,Long userId){
	    JSONObject json=new JSONObject();
	    List<Object> listInfo=ExcelUtil.getInstance().readExcel2ObjsByInputStream(inputStream, ImportIpAddress.class);
	    	if(listInfo==null||listInfo.size()==0){
				 json.put("info", "无数据插入");
				 return json;
			}
	    		    	
	    	for(int i=0;i<listInfo.size();i++){	    		
	    		ImportIpAddress importIpAddress=(ImportIpAddress)listInfo.get(i);
	    	if(importIpAddress.getIp()!=null){		    		
	    		List<JSONObject> temps = ipAddressMapper.getIp(importIpAddress.getIp());
	    		if(temps!=null && temps.size()>0){ 
	    			json.put(importIpAddress.getIp(), "IP:"+importIpAddress.getIp()+" 已经存在");
	    			continue;
	    		}
	    		Long id = sequenceMapper.getSequence("ip_address_id").longValue();
	    		importIpAddress.setId(id);
	    		importIpAddress.setState("0");
	    		importIpAddress.setCreateUser(userId);
	    		importIpAddress.setNetType(dictMapper.getDictKeyByIdAndValue("net_type",importIpAddress.getNetType()));
	    		networkExcelServer.exportIpAddress(importIpAddress);
	    	}
	    	}
	    	json.put("info","sucess");
			return json;
	    }	 
}
